package com.mall.vo;

import lombok.Data;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 购物车VO
 * 
 * @author Mall Team
 */
@Data
public class CartVO {
    
    private Long id;
    private Long userId;
    private Long productId;
    private String productName;
    private String productImage;
    private BigDecimal productPrice;
    private Integer quantity;
    private Boolean checked;
    private BigDecimal totalAmount;  // 小计金额
    private Integer stock;           // 库存数量
    private Integer status;          // 商品状态
    private LocalDateTime createTime;
    private LocalDateTime updateTime;
    
    /**
     * 购物车汇总VO
     */
    @Data
    public static class CartSummaryVO {
        private List<CartVO> cartItems;
        private Integer totalCount;           // 总数量
        private Integer checkedCount;         // 选中数量
        private BigDecimal totalAmount;       // 总金额
        private BigDecimal checkedAmount;     // 选中金额
        private Boolean allChecked;           // 是否全选
        
        /**
         * 计算汇总信息
         */
        public void calculateSummary() {
            if (cartItems == null || cartItems.isEmpty()) {
                totalCount = 0;
                checkedCount = 0;
                totalAmount = BigDecimal.ZERO;
                checkedAmount = BigDecimal.ZERO;
                allChecked = false;
                return;
            }
            
            totalCount = cartItems.size();
            checkedCount = 0;
            totalAmount = BigDecimal.ZERO;
            checkedAmount = BigDecimal.ZERO;
            
            for (CartVO item : cartItems) {
                // 计算小计
                BigDecimal itemTotal = item.getProductPrice().multiply(new BigDecimal(item.getQuantity()));
                item.setTotalAmount(itemTotal);
                totalAmount = totalAmount.add(itemTotal);
                
                if (item.getChecked()) {
                    checkedCount++;
                    checkedAmount = checkedAmount.add(itemTotal);
                }
            }
            
            allChecked = checkedCount > 0 && checkedCount.equals(totalCount);
        }
    }
}